/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package test.hibernate.dao;

import java.util.List;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import test.hibernate.pojo.Customer;

/**
 *
 * @author mamat-dev
 */
@Repository("CustomerDao")
public class CustomerDao extends HibernateDaoSupport {

    @Autowired
    public void setSession(SessionFactory sessionFactory) {
        super.setSessionFactory(sessionFactory);
    }

    public List<Customer> getCustomerAll(){

        return getHibernateTemplate().findByNamedQuery("Customer.findAll");
    }

    public List<Customer> getCustomerId(int id){
        return getHibernateTemplate().findByNamedQuery("Customer.findById",new Object[]{id});
    }

    public int getLastId(){
        int lastId = 0;
        List<Customer> list = getSession().createQuery("FROM Customer c ORDER BY c.id DESC").
                setMaxResults(1).list();
        Customer customer = (Customer)list.get(0);
        lastId = customer.getId();
        return lastId;
    }

    public void addCustomer(Customer c){
        getHibernateTemplate().saveOrUpdate(c);
    }

    public void editCustomer(Customer c){
        getHibernateTemplate().update(c);
    }

    public void deleteCustomer(Customer c){
        getHibernateTemplate().delete(c);
    }
}
